Just like the sun rises and
sets containers start and shut down. There's no escape from that reality. But let's take a look at how
a container shutdown works, first I'd like to reassure you. Cloud Run never stops a container while
it is serving requests under normal circumstances. However, things can go wrong and when they do the container takes
down all of its in flight requests. Those other requests will
fail with an error as well. I think you'll agree that this is
something you generally want to avoid. Let's take a look at two cases where
in a container can stop suddenly while it is handling requests,
your application exits. For instance due to an error
in your application code or the container exceeds the memory limit,
the default of which is 250 megabyte. The memory limit is a soft limit. This means your application can
claim memory over the limit. Cloud to run notices that and
stops the container forcefully. In this case,
cloud run outputs a warning in the logs. You should watch those for
messages because every time that happens, several innocent requests are also
cancelled on the cloud run. You can adjust the memory limit and CPU. Available to your container. Memory can even be adjusted
to custom sizes in the range of 128 MB to 8GB if you wish. It's likely that those limits
continue to be raised in future. We want you to be able to run all
manner of workloads on Cloud Run even the ones that use a lot
of system resources. One notable fact is that
containers with four CPUs need a minimum of two gigabytes of RAM. Now let's look at the regular shutdown. Cloud Run can shut down idle containers
since they are not handling requests. You can't control when
a shutdown happens but you can register a lifecycle hook to make
sure you can handle a shutdown gracefully. This is how a lifecycle hook works. You build your application to
handle the Sig term signal, which is a Linux colonel feature and
most programming language have a library available that lets you hook into that
signal as cloud run, initiate shutdown. It sends the SIGTERM
signal to your program. It also lets you know if your
application is handling the signal and when it doesn't,
if your application handles the signal, Cloud Run gives the container
10 seconds to stop gracefully, during those 10 seconds the container
has full access to the CPU. If your application does
not handle the signal, Cloud Run immediately stops
the container forcefully. In this case the process just stops and
disappears, stopping gracefully allows your
application time to clean up examples of things your application can
do are closing open TCP connections. Most downstream systems are slow
to time out idling connections and if your application experiences
a lot of scaling up and down, you might hit maximum connection
errors if you don't close connections, flush any buffers you have with for
instance telemetry data or any other data you batch before sending. This is data you would lose otherwise. Finally, it's useful to write a log
that helps with debugging later on. To summarize Cloud Run keeps a copy
of your container image in its internal storage to
start containers faster. This way large container images low,
just as quickly as tiny container images. As soon as your application listens for
TCP connections, Cloud Run decides it's ready to handle web requests and
starts to forward them to your container. There is no way to control when a
container stops, but in order to shut down gracefully, your application should
catch the six term signal when it does. It gets 10 seconds to
tie up all loose ends. Finally, Cloud Run never stops
a container that is handling requests, but application crashes and memory limits can bring even a container
down that is serving requests and that will also include all in flight
requests brought down as well.